%% Parameters
tic
clc
clear all

ticker = 'MSN';

from_time = 2007;
to_time = 2012;

normalize_type = 'quar';
% normalize_type = 'ratio';

gap_TA = 10;
gap_ret = 5;   % Tinh return gap_ret ngay (1-4: 5 gap_ret)
window = 37;
time_lag = 2;

tool_FA = 'NN';
% tool_FA = 'step';
hiddenLayerSize_FA = 20;

%% Get security id of all stocks in same sector, FA data and normalize
stock_id = get_info_from_database('sec_id', ticker);
result = get_info_from_database('security-info', stock_id);
sector_id = result.sector_id;

all_sec_id_ok = get_sec_in_same_sector(sector_id);
% Get all id 
sub_sec_id_ok = all_sec_id_ok(1:end,1);

dataX = get_FI_from_security(sub_sec_id_ok,from_time,to_time);

[dataX_normalize, ~, ~ ] = ...
    normalize_data(dataX, normalize_type, stock_id,1,1,2,1,1);

%% Preparation works for FA
[mat_X_FA, mat_Y_FA, mat_Y_FA_vol] = ...
    prepare_FA(dataX_normalize,gap_ret,time_lag);

%% Predict FA average return for 1 stock

% FA stepwise
stepwise(mat_X_FA,mat_Y_FA);

data_FA = dataX_normalize(find(dataX_normalize(:,1) == stock_id),:);
data_fa = data_FA(:,4:end);

% Chose neural network or stepwise
if strcmp(tool_FA, 'NN') == 1
        net = predict_return(tool_FA,mat_X_FA,mat_Y_FA,...
            hiddenLayerSize_FA);
        
        ret_FA = net(data_fa')';
    else
        [coeff, inmodel] = predict_return(tool_FA,mat_X_FA,mat_Y_FA,...
            hiddenLayerSize_FA);
        
        ret_FA = data_fa(:,inmodel) * coeff(inmodel)';
end
nntraintool('close');
%% Preparation works for TA

[mat_X_TA, residual, mat_Y_TA, ~ , ~ , ~ , ~ ] = ...
    prepare_TA(stock_id, data_FA, ...
    ret_FA, gap_TA, gap_ret, window, time_lag);
 
%% Explain relationships btw TA indicators & residual returns
% TA stepwise
stepwise(mat_X_TA(:,[1:3,6:8,11:end]),residual);

% For Neural Network
% Open NN file, fill in the following blank to see the results
% inputs = mat_X_TA'
% target = residual'